package cn.itcast.bos.web.action.transit;

import cn.itcast.bos.domain.transit.TransitInfo;
import cn.itcast.bos.service.transit.TransitInfoService;
import cn.itcast.bos.web.action.common.BaseAction;
import com.alibaba.fastjson.JSONArray;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller
@Namespace("/")
@ParentPackage("struts-default")
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class TransitInfoAction extends BaseAction<TransitInfo> {

    private String wayBillIds;

    public void setWayBillIds(String wayBillIds) {
        this.wayBillIds = wayBillIds;
    }

    @Autowired
    private TransitInfoService transitInfoService;

    @Action(value = "transitInfo_create")
    public void create() {
        Map<String, Object> mapData = new HashMap<>();
        try {
            transitInfoService.create(wayBillIds);
            mapData.put("success", true);
            mapData.put("msg", "操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            mapData.put("success", false);
            mapData.put("msg", "操作失败");
        }
        String json = JSONArray.toJSONString(mapData);
        ServletActionContext.getResponse().setCharacterEncoding("utf-8");
        try {
            ServletActionContext.getResponse().getWriter().write(json);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Action(value = "transitInfo_list")
    public void showPage(){
        Pageable pageable = new PageRequest(page, rows);
        Page<TransitInfo> pageData = transitInfoService.findAll(pageable);
        writeJson(pageData);
    }
}
